<?php

namespace App\Http\Controllers\admin\sign;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class SignController extends Controller
{
    public function signIndex()
    {
        return view('sign.sign_index');
    }

    public function createSign()
    {
        $uid = 1;//用户id
        $day = date('d');// 日
        $key = 'user_' . date('m') . '_' . $uid;//月
        $yes = date('d', strtotime('-1 day'));//$yes = 26 号
        $res = Redis::getbit($key, $day); //$day = 27 号
        $count = 1;
        if ($res == 1) {
            //统计一个月签到总数
            echo '今天已经签到,这个月总共签到' . Redis::bitcount($key, 1) . "\n";

        } else {
            //判断昨天有没有签到   1签到 0没签到
            if (Redis::getbit($key, $yes) == 0) {
                Redis::setbit($key, $day, 1);
                return "签到成功";
            } else {
                //判断连续签到
                Redis::setbit($key, $day, 1);
                for ($i = intval($day) - 1; $i > 0; $i--) {
                    if (Redis::getbit($key, $i) == 0) {
                        echo '连续签到成功,已经连续签到' . $count . '天';
                        return false;
                    } else {
                        $count += 1;
                    }
                }


            }
        }

    }

    public function repairSign()
    {
        $uid = \request()->get('uid');
        $day1 = \request()->get('day');
//        dd($day1);
        $mon = date('m');
        $day2 = date('d');
        if ($day1 >= $day2) {
            return "不能补签未来的签到";
        }
        $int1 = intval($day1);
        $int2 = intval($day2);
        $yes = date('d', strtotime('-1 day'));
        $key = 'user_' . $mon . '_' . $uid;//月
        $count = 1;
        if (Redis::getbit($key, $int1) == 1) {
            return '第' . $day1 . '天已签到';
        } else {
            Redis::setbit($key, $int1, 1);
            //计算补签之后到今天的连签天数
            for ($i = $int2 - 1; $i >= $int1; $i--) {
                if (Redis::getbit($key, $i) == 0) {
                    echo "已连续签到" . $count . '天';
                    return false;
                } else {
                    $count++;
                }
            }
            //计算补签那天以及之前是否连签
            for ($i = $int1 - 1; $i >= 0; $i--) {
                if (Redis::getbit($key, $i) == 0) {
                    echo "已连续签到" . $count . '天';
                    return false;
                } else {
                    $count++;
                }
            }
        }

    }
}
